/**
 * 
 */
package com.ideal.trsdata.service;


import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.stereotype.Service;

import com.ideal.trsdata.model.TrsModel;
import com.ideal.trsdata.util.SouthProv;
import com.ideal.trsdata.util.TrsDataConfig;

/**
 * 
 * <p>
 * Title: ImportTrsService.java
 * </p>
 * <p>
 * Description: 
 * </p>
 * @author zhang_zhi
 * @version 1.0
 * @created Jul 20, 2011
 */
@Service
@Scope("prototype")
public class ImportTrsService {
	@Autowired
	private TrsDataImportService trsDataService;
	
	protected static final Log logger = LogFactory.getLog(ImportTrsService.class);
	
	
	
	//分省 分页 每次10条数据 
	public void processTrs(SouthProv prov, int flag){
		logger.info("star process Trs................ ");
		List<TrsModel> trsList=null;
		TrsModel tmp=new TrsModel();
		Integer start=1;
		Integer end=10;
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("flag", flag);
		map.put("start",start);
		map.put("end",end);
		map.put("partition","part"+prov.getProv());
		//trsDataService.updateTmpTrsErr(map);
		trsList=trsDataService.getTrsTmp(map);
		while(trsList!=null&&trsList.size()>0){		
			for(TrsModel model:trsList){
				try {
					tmp=model;
					trsDataService.importTrs(model);	
				} catch (Exception e) {
					TrsDataConfig.log.error(model.getSerialid()+":"+e.getMessage());
					trsDataService.insertDataLog(tmp,trsDataService.getWarn("1","Trs处理失败:[".concat(e.getMessage()).concat("]"),"1"));
				}finally{
					//更新中间表的opter_type='1'
					trsDataService.updateTmpTrs(tmp);
				}
			}
			trsList=trsDataService.getTrsTmp(map);
		}
		logger.info("end process Trs................ ");
		
	}

	//分页 每次10条数据 
	public void processTrs(){
		logger.info("star process Trs................ ");
		List<TrsModel> trsList=null;
		TrsModel tmp=new TrsModel();
		Integer start=1;
		Integer end=10;
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("start",start);
		map.put("end",end);
		
		if(TrsDataConfig.FindFlag==0){
			TrsDataConfig.endTime= Calendar.getInstance().getTime();
			map.put("endTime",TrsDataConfig.endTime);
		}else{
			map.put("endTime",new Date(System.currentTimeMillis()));
			map.put("startTime", TrsDataConfig.endTime);
		}
		TrsDataConfig.FindFlag=2;
		trsList=trsDataService.getTrsTmp(map);
		while(trsList!=null&&trsList.size()>0){		
			for(TrsModel model:trsList){
				try {
					tmp=model;
					trsDataService.importTrs(model);	
				} catch (Exception e) {
					TrsDataConfig.log.error(e.getMessage());
					e.printStackTrace();
					trsDataService.insertDataLog(tmp,trsDataService.getWarn("1","Trs处理失败:[".concat(e.getMessage()).concat("]"),"1"));
				}finally{
					trsDataService.updateTmpTrs(tmp);
				}
			}
			trsList=trsDataService.getTrsTmp(map);
		}
		TrsDataConfig.FindFlag=2;
		logger.info("end process Trs................ ");
		
	}
	
	//每次50条数据 
	public void processTrs3(){
			logger.info("star process Trs................ ");
			List<TrsModel> trsList=null;
			TrsModel tmp=new TrsModel();
		
			trsList=trsDataService.getTrsTmp();
			while(trsList!=null&&trsList.size()>0){		
				for(TrsModel model:trsList){
					try {
						tmp=model;
						trsDataService.importTrs(model);	
					} catch (Exception e) {
						TrsDataConfig.log.error(e.getMessage());
						e.printStackTrace();
						trsDataService.insertDataLog(tmp,trsDataService.getWarn("1","Trs处理失败:[".concat(e.getMessage()).concat("]"),"1"));
					}finally{
						trsDataService.updateTmpTrs(tmp);
					}
				}
				trsList=trsDataService.getTrsTmp();
			}
			logger.info("end process Trs................ ");
	}
	
	
	//单查,根据时间标记
	public void processTrs2(){
		TrsModel tmp=new TrsModel();
		while(true){
			try{
				tmp=trsDataService.getSigTmp();
			}catch(Exception e){
				e.printStackTrace();
				TrsDataConfig.log.error(e.getMessage());	
			}
			if(tmp!=null){
				try {	
						trsDataService.importTrs(tmp);	
				} catch (Exception e) {
					TrsDataConfig.log.error(e.getMessage());
					e.printStackTrace();
					trsDataService.insertDataLog(tmp,trsDataService.getWarn("1","Trs处理失败:[".concat(e.getMessage()).concat("]"),"1"));
				}finally{
					trsDataService.updateTmpTrs(tmp);
				}
			}
		}
	}
	
	//测试结果....每次10条比较快
	public static void main(String[] args) {
		ApplicationContext ctx=new FileSystemXmlApplicationContext("conf/bean.xml");
		ImportTrsService mp=(ImportTrsService)ctx.getBean("importTrsService");
		mp.processTrs();
	}
}
